package net.lzzy.practices.aspects;

import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.User;
import net.lzzy.practices.utils.AspectUtils;
import net.lzzy.practices.utils.AuthUtils;
import net.lzzy.practices.web.models.ApiResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author 渊何
 */
@Component
@Aspect
public class ApplicantControllerAspect {
    @Pointcut("execution(* net.lzzy.practices.web.course.ApplicantApiController.getApplyingStudents(..)) || " +
            "execution(* net.lzzy.practices.web.course.ApplicantApiController.getDeclinedStudents()) || " +
            "execution(* net.lzzy.practices.web.course.ApplicantApiController.getEnrolledStudents()) ")
    private void teacherAspect(){}

    @Pointcut("execution(* net.lzzy.practices.web.course.ApplicantApiController.getApplyingCourses(..)) || " +
            "execution(* net.lzzy.practices.web.course.ApplicantApiController.getDeclinedCourses()) || " +
            "execution(* net.lzzy.practices.web.course.ApplicantApiController.getEnrolledCourses()) ")
    private void studentAspect(){}

    @Around("teacherAspect()")
    public ApiResult aroundTeacher(ProceedingJoinPoint point) throws Throwable{
        String token = AspectUtils.getToken(point);
        if (AuthUtils.illegalUser(token, new int[]{User.TYPE_TEACHER})){
            return new ApiResult(false, Messages.NO_PERMISSION.toString(), null);
        }
        return (ApiResult) point.proceed();
    }

    @Around("studentAspect()")
    public ApiResult aroundStudent(ProceedingJoinPoint point) throws Throwable{
        String token = AspectUtils.getToken(point);
        if (AuthUtils.illegalUser(token, new int[]{User.TYPE_STUDENT})){
            return new ApiResult(false, Messages.NO_PERMISSION.toString(), null);
        }
        return (ApiResult) point.proceed();
    }
}
